package com.fjwt.gz.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.db.entity.ExchangeBlacklistEntity;
import com.fjwt.gz.service.mapper.ExchangeBlacklistMapper;
import com.fjwt.gz.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 积分兑换黑名单，无法参与 服务实现类
 * </p>
 *
 * @author wangjie
 * @since 2025-05-12
 */
@Slf4j
@Service
public class ExchangeBlacklistService extends ServiceImpl<ExchangeBlacklistMapper, ExchangeBlacklistEntity> {

    public List<ExchangeBlacklistEntity> getExchangeBlacklistEntities(Long scoreExchangeId, Long redisOutTime){
        //活动黑名单redis key  先从redis中获取，如没有从数据库中查询，插入redis中
        String scoreExchangeBlacklistKey  =  String.format("%s%s:%s", Constants.REDIS_ACTIVITY.SCORE_EXCHANGE, scoreExchangeId, Constants.REDIS_ACTIVITY.SCORE_EXCHANGE_BLACKLIST);
        List<ExchangeBlacklistEntity> scoreExchangeBlacklistEntities = RedisUtil.getObject(scoreExchangeBlacklistKey, List.class);
        log.info("积分ID：{}，积分黑名单redis key:{},redis中查询结果：{}",scoreExchangeId, scoreExchangeBlacklistKey, JsonUtils.toJson(scoreExchangeBlacklistEntities));
        if (CollUtil.isNotEmpty(scoreExchangeBlacklistEntities)){
            return scoreExchangeBlacklistEntities;
        }
        scoreExchangeBlacklistEntities = baseMapper.selectList(ExchangeBlacklistEntity.gw()
                .eq(ExchangeBlacklistEntity::getScoreExchangeId, scoreExchangeId));
        log.info("积分ID：{}，数据库查询结果：{}",scoreExchangeId, JsonUtils.toJson(scoreExchangeBlacklistEntities));
        //放置Redis中
        RedisUtil.set(scoreExchangeBlacklistKey, scoreExchangeBlacklistEntities, redisOutTime, TimeUnit.MILLISECONDS);
        return scoreExchangeBlacklistEntities;
    }
}
